//
//  PlayView.m
//  27_Block_播放按钮
//
//  Created by 徐昊 on 16/8/19.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
@interface PlayView ()
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *lastButton;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);
@property (nonatomic, copy) void (^next)(UIButton *next);
@property (nonatomic, copy) void (^last)(UIButton *last);
@end

@implementation PlayView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)setUp {
    _playButton = [self buttonImageWithName:@"player_btn_play_normal"];
    _nextButton = [self buttonImageWithName:@"player_btn_next_normal"];
    _lastButton = [self buttonImageWithName:@"player_btn_pre_normal"];
    [_playButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    [_nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    [_lastButton addTarget:self action:@selector(lastButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
}
- (void)next:(void (^)(UIButton *))next {
    self.next = next;
}
- (void)last:(void (^)(UIButton *))last {
    self.last = last;
}
- (void)playButton:(UIButton *)playButton {
    [playButton setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [playButton removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    [playButton addTarget:self action:@selector(pauseButton:) forControlEvents:UIControlEventTouchUpInside];
    self.play(playButton);
}
- (void)pauseButton:(UIButton *)pauseButton {
    [pauseButton setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [pauseButton removeTarget:self action:@selector(pauseButton:) forControlEvents:UIControlEventTouchUpInside];
    [pauseButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(pauseButton);
}
- (void)nextButton:(UIButton *)nextButton {
    self.next(nextButton);
}
- (void)lastButton:(UIButton *)lastButton {
    self.last(lastButton);
}

- (UIButton *)buttonImageWithName:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self addSubview:button];
    button.tintColor = [UIColor greenColor];
    return  button;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    _lastButton.h_width = 60;
    _lastButton.h_height = 60;
    _lastButton.h_centerY = _playButton.h_centerY;
    _lastButton.h_centerX = _playButton.h_centerX - (_lastButton.h_width / 2 + _playButton.h_width / 2 + 30);
    _nextButton.h_width = 60;
    _nextButton.h_height = 60;
    _nextButton.h_centerY = _playButton.h_centerY;
    _nextButton.h_centerX = _playButton.h_centerX + (_lastButton.h_width / 2 + _playButton.h_width / 2 + 30);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
